package com.nowcoder.community.config;

import com.nowcoder.community.quartz.AlphaJob;
import com.nowcoder.community.quartz.PostScoreRefreshJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * 配置 初始化到数据库
 * 以后(Quartz访问数据库)不再使用
 * @author 花木凋零成兰
 * @date 2024/3/30 22:32
 */
@Configuration
public class QuartzConfig {

    /**
     * FactoryBean可简化Bean的实例化过程：
     *     1、通过FactoryBean封装Bean的实例化过程
     *     2、将FactoryBean装配到Spring容器里
     *     3、将FactoryBean注入给其他的Bean
     *     4、那么该Bean得到的是FactoryBean所管理的对象实例
     *
     */

    /**
     * 配置JobDetail
     * @return
     */
    // @Bean
    public JobDetailFactoryBean alphaJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(AlphaJob.class);
        factoryBean.setName("alphaJob");    // 给Bean取名
        factoryBean.setGroup("alphaJobGroup");  // 给任务分配组
        factoryBean.setDurability(true);    // 声明任务为持久保存
        factoryBean.setRequestsRecovery(true);  // 应用是否可回复
        return factoryBean;
    }

    /**
     * 配置Trigger() SimpleTriggerFactory较为简单 比如间隔十分钟、CronTriggerFactoryBean更复杂，比如每个月执行
     * @param alphaJobDetail
     * @return
     */
    // @Bean
    public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(alphaJobDetail);   // 声明对哪个JobDetail
        factoryBean.setName("alphaTrigger");
        factoryBean.setGroup("alphaTriggerGroup");
        factoryBean.setRepeatInterval(3000);    // 间隔多少毫秒执行
        factoryBean.setJobDataMap(new JobDataMap());    // Trigger底层存储的对象状态
        return factoryBean;
    }

    /**
     * 刷新帖子分数
     * 配置JobDetail
     * @return
     */
    @Bean
    public JobDetailFactoryBean postScoreRefreshJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        factoryBean.setName("postScoreRefreshJob");    // 给Bean取名
        factoryBean.setGroup("communityJobGroup");  // 给任务分配组
        factoryBean.setDurability(true);    // 声明任务为持久保存
        factoryBean.setRequestsRecovery(true);  // 应用是否可回复
        return factoryBean;
    }

    /**
     * 配置刷新帖子分数 执行
     * 配置Trigger() SimpleTriggerFactory较为简单 比如间隔十分钟、CronTriggerFactoryBean更复杂，比如每个月执行
     * @param postScoreRefreshJobDetail
     * @return
     */
    @Bean
    public SimpleTriggerFactoryBean postScoreRefreshTrigger(JobDetail postScoreRefreshJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefreshJobDetail);   // 声明对哪个JobDetail
        factoryBean.setName("postScoreRefreshTrigger");
        factoryBean.setGroup("communityTriggerGroup");
        factoryBean.setRepeatInterval(1000 * 60 * 5);    // 间隔多少秒执行
        factoryBean.setJobDataMap(new JobDataMap());    // Trigger底层存储的对象状态
        return factoryBean;
    }

}
